<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: module_export.php 66 2013-09-21 11:59:41Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 66 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:59:41 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

require_once(DIR_WS_FUNCTIONS.'export_functions.php');

if(!is_writeable(DIR_FS_CATALOG.'export/'))
	$message_stack->add(ERROR_EXPORT_FOLDER_NOT_WRITEABLE, 'error');

$module_type = 'export';
$module_directory = DIR_WS_MODULES.'export/';
$module_key = 'MODULE_EXPORT_INSTALLED';
$file_extension = '.php';
define('HEADING_TITLE', HEADING_TITLE_MODULES_EXPORT);

if(isset($_GET['error'])) {
	$map='error';
	if($_GET['kind']=='success')
		$map = 'success';
	$message_stack->add($_GET['error'], $map);
}

switch ($_GET['action']) {
    case 'save':
		if (is_array($_POST['configuration'])) {
			if (count($_POST['configuration'])) {
				while (list($key, $value) = each($_POST['configuration'])) {
					$db->db_query("UPDATE ".TABLE_CONFIGURATION." set configuration_value = '".$value."' WHERE configuration_key = '".$key."'");
					if(strpos($key, 'FILE') !== false)
						$file = $value;
				}
			}
		}

		$class = basename($_GET['module']);
		include($module_directory.$class.$file_extension);

		if($class == 'image_processing_step'){
			$start = $_GET['start'];
			if($start == '')
				$start = 0;

			$module = new $class;
			$module->process($file, $start);

		} elseif($class == 'image_processing_new_step') {
			$start = $_GET['start'];
			if($start == '')
				$start = 0;

			$module = new $class;
			$module->process($file, $start);

		} elseif($class == 'image_processing_new_step2') {
			$module = new $class;
			$module->process($file);

		} else {
			$module = new $class;
			$module->process($file);
			redirect(href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$class));
		}
	break;

    case 'install':
    case 'remove':
		$file_extension = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '.'));
		$class = basename($_GET['module']);
		if(file_exists($module_directory.$class.$file_extension)) {
			include($module_directory.$class.$file_extension);
			$module = new $class;
			if($_GET['action'] == 'install')
				$module->install();
			elseif ($_GET['action'] == 'remove')
				$module->remove();
		}
		redirect(href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$class));
	break;
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellspacing="0" cellpadding="0">
  <tr>
    <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
		<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	</td>
    <td class="boxCenter" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%">
        <table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
		  <tr>
		    <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
		  </tr>
		</table> </td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top">
             <table width="100%" class="dataTable" cellspacing="0" cellpadding="0">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent" colspan="2"><?php echo TABLE_HEADING_MODULES; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
				<?php
					$file_extension = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '.'));
					$directory_array = array();
					if ($dir = @dir($module_directory)) {
						while ($file = $dir->read())
							if(!is_dir($module_directory.$file))
								if(substr($file, strrpos($file, '.')) == $file_extension)
									$directory_array[] = $file;

						sort($directory_array);
						$dir->close();
					}

					$installed_modules = array();

					for ($i = 0, $n = sizeof($directory_array); $i < $n; $i++) {
						$file = $directory_array[$i];

						include($module_directory.$file);

						$class = substr($file, 0, strrpos($file, '.'));
						if(class_exists($class)) {
							$module = new $class;
							if ($module->check() > 0) {
								if ($module->sort_order > 0)
									$installed_modules[$module->sort_order] = $file;
								else
									$installed_modules[] = $file;
							}

							if (((!$_GET['module']) || ($_GET['module'] == $class)) && (!$mInfo)) {
								$module_info = array('code' => $module->code,
													'title' => $module->title,
													'img' => $module->img,
													'description' => $module->description,
													'status' => $module->check());

								$module_keys = $module->keys();
								$keys_extra = array();
								for($j = 0, $k = sizeof($module_keys); $j < $k; $j++) {
									$key_value = $db->db_query("select configuration_key,configuration_value, use_function, set_function from ".TABLE_CONFIGURATION." WHERE configuration_key = '".$module_keys[$j]."'");

									if($key_value->fields['configuration_key'] !='')
										$keys_extra[$module_keys[$j]]['title'] = constant(strtoupper($key_value->fields['configuration_key'] .'_TITLE'));
									$keys_extra[$module_keys[$j]]['value'] = $key_value->fields['configuration_value'];
									if($key_value->fields['configuration_key'] !='')
										$keys_extra[$module_keys[$j]]['description'] = constant(strtoupper($key_value->fields['configuration_key'] .'_DESC'));
									$keys_extra[$module_keys[$j]]['use_function'] = $key_value->fields['use_function'];
									$keys_extra[$module_keys[$j]]['set_function'] = $key_value->fields['set_function'];
								}

								$module_info['keys'] = $keys_extra;

								$mInfo = new objectInfo($module_info);
							}


							if((is_object($mInfo)) && ($class == $mInfo->code)) {
								if($module->check() > 0) {
									echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'" onclick="document.location.href=\''.href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$class.'&action=edit').'\'">'."\n";
								} else
									echo '<tr class="dataTableRowSelected">'."\n";

							} else {
								echo '              <tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''.$f.'\'" onclick="document.location.href=\''.href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$class).'\'">'."\n";
							}
							?>
							<td align="center" width="1px" style="background:#fff">
								<?php echo ($module->img !='')?'<img src="'.$module->img.'" alt="'.$module->title.'" />':''; ?>
							</td>
							<td>
								<?php echo $module->title; ?>
							</td>
							<td align="right">
								<?php
									if((is_object($mInfo)) && ($class == $mInfo->code) ) {
										echo image(DIR_WS_IMAGES.'icon_arrow_right.gif');
									} else {
										echo '<a href="'.href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$class).'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>';
									}
								?>
							</td>
							</tr>
							<?php
						}
					}

					ksort($installed_modules);
					$check = $db->db_query("select configuration_value from ".TABLE_CONFIGURATION." WHERE configuration_key = '".$module_key."'");
					if($check->_numOfRows > 0) {
						if($check->fields['configuration_value'] != implode(';', $installed_modules))
							$db->db_query("UPDATE ".TABLE_CONFIGURATION." set configuration_value = '".implode(';', $installed_modules)."', last_modified = now() WHERE configuration_key = '".$module_key."'");

					} else {
						$db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ( '".$module_key."', '".implode(';', $installed_modules)."','6', '0', now())");
					}
					?>
	              <tr>
	                <td colspan="3" class="smallText"><?php echo TEXT_MODULE_DIRECTORY.' admin/'.$module_directory; ?></td>
	              </tr>
	            </table></td>
				<?php
				  $heading = array();
				  $contents = array();
				  switch ($_GET['action']) {
				    case 'edit':
				      $keys = '';
				      reset($mInfo->keys);
				      while (list($key, $value) = each($mInfo->keys)) {
					 // if($value['description']!='_DESC' && $value['title']!='_TITLE'){
				        $keys .= '<b>'.$value['title'].'</b><br />'. $value['description'].'<br />';
					//	}
				        if ($value['set_function']) {
				          eval('$keys .= '.$value['set_function']."'".$value['value']."', '".$key."');");
				        } else {
				          $keys .= draw_input_field('configuration['.$key.']', $value['value']);
				        }
				        $keys .= '<br /><br />';
				      }
				      $keys = substr($keys, 0, strrpos($keys, '<br /><br />'));

				      $heading[] = array('text' => '<b>'.$mInfo->title.'</b>');
				      $class = substr($file, 0, strrpos($file, '.'));
				      $module = new $_GET['module'];
				      $contents = array('form' => draw_form('modules', FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$_GET['module'].'&action=save','post'));
				      $contents[] = array('text' => $keys);
				      // display module fields
				      $contents[] = $module->display();

				      break;

				    default:
				      $heading[] = array('text' => '<b>'.$mInfo->title.'</b>');

				      if ($mInfo->status == '1') {
				        $keys = '';
				        reset($mInfo->keys);
				        while (list(, $value) = each($mInfo->keys)) {
				          $keys .= '<b>'.$value['title'].'</b><br />';
				          if ($value['use_function']) {
				            $use_function = $value['use_function'];
				            if (preg_match('/->/', $use_function)) {
				              $class_method = explode('->', $use_function);
				              if (!is_object(${$class_method[0]})) {
				                include(DIR_WS_CLASSES.$class_method[0].'.php');
				                ${$class_method[0]} = new $class_method[0]();
				              }
				              $keys .= call_function($class_method[1], $value['value'], ${$class_method[0]});
				            } else {
				              $keys .= call_function($use_function, $value['value']);
				            }
				          } else {
						  if(strlen($value['value']) > 30) {
						  $keys .=  substr($value['value'],0,30).' ...';
						  } else {
				            $keys .=  $value['value'];
							}
				          }
				          $keys .= '<br /><br />';
				        }
				        $keys = substr($keys, 0, strrpos($keys, '<br /><br />'));

				        $contents[] = array('align' => 'center', 'text' => '<a class="button" onClick="this.blur();" href="'.href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$mInfo->code.'&action=remove').'">'.BUTTON_MODULE_REMOVE.'</a> <a class="button" onClick="this.blur();" href="'.href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$mInfo->code.'&action=edit').'">'.BUTTON_START.'</a>');
				        $contents[] = array('text' => '<br />'.$mInfo->description);
				        $contents[] = array('text' => '<br />'.$keys);
				      } else {
				        $contents[] = array('align' => 'center', 'text' => '<a class="button" onClick="this.blur();" href="'.href_link(FILENAME_MODULE_EXPORT, 'set='.$_GET['set'].'&module='.$mInfo->code.'&action=install').'">'.BUTTON_MODULE_INSTALL.'</a>');
				        $contents[] = array('text' => '<br />'.$mInfo->description);
				      }
				      break;
				  }

				  if ( (not_null($heading)) && (not_null($contents)) ) {
				    echo '            <td width="25%" class="border" valign="top">'."\n";

				    $box = new box;
				    echo $box->infoBox($heading, $contents);

				    echo '            </td>'."\n";
				  }
				?>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>